﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NTUTMessagerAPI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SocketAPI socketAPI = new SocketAPI();

        private void Form1_Load(object sender, EventArgs e)
        {
            socketAPI.ConnectListChangeEvent += ConnectListChange;
            socketAPI.ConnectListAddEvent += SomeoneOnline;
            socketAPI.ConnectListRemoveEvent += SomeoneOffline;
            socketAPI.GetMessageEvent += GetMessage;
            socketAPI.GetCommandEvent += GetCommand;

            Diffie_hellman diff1 = new Diffie_hellman();
            Diffie_hellman diff2 = new Diffie_hellman();

            diff1.PrivateKey = 44;
            diff2.PrivateKey = 29;

            diff1.OtherPublicKey = diff2.PublicKey;
            diff2.OtherPublicKey = diff1.PublicKey;

            Console.WriteLine("diff1 " + diff1.ShareSecret);
            Console.WriteLine("diff2 " + diff2.ShareSecret);
        }

        public void SomeoneOnline(ConnectPair pair)
        {
            label1.Text = pair.ToString() + "上線了~";
        }


        public void SomeoneOffline(ConnectPair pair)
        {
            label1.Text = pair.ToString() + "下線了....";
        }

        public void GetMessage(ConnectPair pair, String message)
        {
            listBox2.Items.Add(pair.ToString() + "說:");
            listBox2.Items.Add("    " + message);
        }

        public void GetCommand(ConnectPair pair, String command)
        {
            label1.Text = pair + "命令你" + command;
        }

        public void ConnectListChange()
        {
            listBox1.Items.Clear();
            try
            {
                foreach (ConnectPair p in socketAPI.connectedList)
                {
                    listBox1.Items.Add(p);
                }
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            socketAPI.BeginListen();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            socketAPI.ConnectTo(textBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = socketAPI.MyIp.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //socketAPI.PrintAllPair();

            listBox1.Items.Clear();

            foreach (ConnectPair p in socketAPI.connectedList) 
            {
                listBox1.Items.Add(p);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
         

        }

        private void button7_Click(object sender, EventArgs e)
        {
          //  socketAPI.SendStep2((int)numericUpDown1.Value, textBox2.Text);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null) 
            {
                socketAPI.SendMessage(listBox1.SelectedIndex,textBox2.Text);
            }
            
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                socketAPI.SendCommand(listBox1.SelectedIndex, textBox2.Text);
            }
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                socketAPI.SendBytes(listBox1.SelectedIndex, System.Text.Encoding.UTF32.GetBytes(textBox2.Text));
            }

        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                socketAPI.Send_Diffie_Hellman(listBox1.SelectedIndex, Diffie_hellman._random.Next(100));
            }
        }


    }
}
